//
//  ConfirmCouponCollectCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/3.
//

import UIKit

class ConfirmCouponCollectCell: UICollectionViewCell {
    var data: CouponDataModel?
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = .white
        tempView.layer.borderWidth = 1
        tempView.layer.borderColor = Theme.tfBorderColor.cgColor
        tempView.layer.cornerRadius = 8
        tempView.layer.masksToBounds = true
        return tempView
    }()
    var titleLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    var createDateLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var selectBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setup_image(image: UIImage.init(named: "icon_brand_select"))
        tempBtn.backgroundColor = .clear
        return tempBtn
    }()
    
    var tipLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .center
        tempLab.layer.borderWidth = 1
        tempLab.layer.borderColor = Theme.tfBorderColor.cgColor
        tempLab.layer.cornerRadius = 11
        tempLab.layer.masksToBounds = true

        return tempLab
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = Theme.whiteColor
        contentView.addSubview(bkgView)
        bkgView.addSubview(titleLab)
        bkgView.addSubview(createDateLab)
        bkgView.addSubview(tipLab)
        bkgView.addSubview(selectBtn)
        
        selectBtn.addTarget(self, action: #selector(selectBtnAction), for: .touchUpInside)
        
//        titleLab.text = "满1000减200"
//        createDateLab.text = "2023-08-10至2023-08-3"
//        tipLab.text = "仅商品可用"
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(14)
            make.right.equalTo(-30)
            make.height.greaterThanOrEqualTo(20)
        }
        
        createDateLab.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(titleLab.snp.bottom).offset(4)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-30)

        }
        
        tipLab.frame.size.width = 200
        tipLab.sizeToFit()
        let tempW = tipLab.frame.size.width + 20
        tipLab.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(createDateLab.snp.bottom).offset(4)
            make.height.greaterThanOrEqualTo(22)
            make.width.equalTo(tempW)
        }
        
        selectBtn.snp.makeConstraints { make in
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.top.equalTo(14)
            make.right.equalTo(-12)
        }
        
    }
    
    ///
    func setConfirmCouponCollectCell(data: CouponDataModel) {
        self.data = data
        self.titleLab.text = data.couponName ?? ""
        self.createDateLab.text = data.giftTypeStr ?? ""
        
        self.tipLab.text = XTManageCenter.getCouponuByscene(useScene: data.useScene ?? "")
        self.tipLab.frame.size.width = 200
        self.tipLab.sizeToFit()
        let tempW = self.tipLab.frame.size.width
        self.tipLab.snp.updateConstraints { make in
            make.width.equalTo(tempW + 20)
        }
        
        self.setSelectImg()
        self.updateConstraints()
    }
    
    private func setSelectImg(){
        if self.data?.isSelect ?? false {
            self.selectBtn.setup_image(image: UIImage.init(named: "icon_brand_select"))
        }else{
            self.selectBtn.setup_image(image: UIImage.init(named: "icon_brand_normal"))
        }
    }
    
    ///选中回调
    @objc func selectBtnAction(){
        self.data?.isSelect = !(self.data?.isSelect ?? false)
        self.setSelectImg()
        self.data?.selectBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
